/**
 * Copyright 2011-2011 - Members of the Chaupal Community
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.chaupal.Impl.HostEndpoint;

import net.chaupal.API.HostEndpoint.HostEndpoint;
import net.chaupal.API.HostEndpoint.HostEndpointIP;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import net.chaupal.Impl.Tools.Soundness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Defines an IP endpoint access to an host located on the internet, with 
 * a communication protocol.
 */
public class SimpleHostEndpointIP implements HostEndpointIP {
    
    // Logger
    private static final Logger LOG
        = LoggerFactory.getLogger(SimpleHostEndpointIP.class);
    
    // Attributes
    
    private URI hostEndpoint;
    
    public SimpleHostEndpointIP(String inHostEndpoint)
            throws URISyntaxException {
        
        // Sanity
        Soundness.nullParameter(LOG, inHostEndpoint);
        
        // Initialization
        this.hostEndpoint = new URI(inHostEndpoint);
        
    }
    
    public SimpleHostEndpointIP(
            String protocol, Inet4Address hostAddress, int portNumber) {
        
        // Sanity
        Soundness.nullParameter(LOG, protocol, "Null protocol");
        Soundness.nullParameter(LOG, hostAddress, "Null host address");
        Soundness.rangeIncludedInteger(
                LOG, portNumber, 0, 65535, "Invalid port");
        
        try {
            this.hostEndpoint = new URI(protocol, null,
                hostAddress.getHostAddress(), portNumber, null, null, null);
        } catch (URISyntaxException ex) {
            Soundness.unreachableCodeReached(LOG, ex, "");
        }
        
    }
    
    public SimpleHostEndpointIP(
            String protocol, Inet6Address hostAddress, int portNumber) {
        
        // Sanity
        Soundness.nullParameter(LOG, protocol, "Null protocol");
        Soundness.nullParameter(LOG, hostAddress, "Null host address");
        Soundness.rangeIncludedInteger(
                LOG, portNumber, 0, 65535, "Invalid port");
        
        try {
            this.hostEndpoint = new URI(protocol, null,
                hostAddress.getHostAddress(), portNumber, null, null, null);
        } catch (URISyntaxException ex) {
            Soundness.unreachableCodeReached(LOG, ex, "");
        }
        
    }

    @Override
    public final String getCommunicationProtocol() {
        return this.hostEndpoint.getScheme();
    }
    
    @Override
    public final URI toURI() {
        return this.hostEndpoint;
    }
    
    @Override
    public final String toString() {
        return this.hostEndpoint.toString();
    }

    @Override
    public final InetAddress getInternetAddress() {

        InetAddress result = null;
        
        try {
            result = InetAddress.getByName(this.hostEndpoint.getHost());
        } catch (UnknownHostException ex) {
            Soundness.unreachableCodeReached(LOG, ex, "");
        }
        
        // Returning result
        return result;
        
    }

    @Override
    public final int getPort() {
        
        return this.hostEndpoint.getPort();
        
    }

    @Override
    public final int compareTo(HostEndpoint o) {
        
        if ( o == null ) return 1;
        
        return this.toString().compareTo(o.toString());
        
    }
    
}